function getTree(data) {
    let parents = data.filter((v) => v.pid == 0)
    let children = data.filter((v) => v.pid !== 0)
    dataToTree(parents, children)

    function dataToTree(parents, children) {
        parents.map((v) => {
            children.map((c, i) => {
                if (c.pid === v.id) {
                    let _c = JSON.parse(JSON.stringify(children))
                    _c.splice(i, 1)
                    dataToTree([c], _c)

                    if (v.children) {
                        v.children.push(c)
                    } else {
                        v.children = [c]
                    }
                }
            })
        })
    }
    return parents
}

export default getTree